package geneticpolyimage;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class Polygon represent a polygon of individual
 *
 * @author igor & mattia
 */
public class Polygon implements Cloneable {

    public int[] xvertices;
    public int[] yvertices;
    public int redvalue;
    public int greenvalue;
    public int bluevalue;
    public int alpha;
    public int vertices;

    /**
     * Generate a ranomd polygon
     * @param numvertices nuber of vertices
     * @param maxx width of image
     * @param maxy heigth of image
     */
    Polygon(int numvertices, int maxx, int maxy) {
        Random r = new Random();
        vertices = numvertices;
        xvertices = new int[numvertices];
        yvertices = new int[numvertices];
        for (int i = 0; i < numvertices; i++) {
            xvertices[i] = r.nextInt(maxx);
            yvertices[i] = r.nextInt(maxy);
        }
        redvalue = r.nextInt(255);
        greenvalue = r.nextInt(255);
        bluevalue = r.nextInt(255);
        alpha = r.nextInt(255);
    }

    /**
     * Print polygon attributes
     */
    public void print() {
        for (int k = 0; k < this.vertices; k++) {
            System.out.print("(");
            System.out.print(xvertices[k]);
            System.out.print(" , ");
            System.out.print(yvertices[k]);
            System.out.print(") ");
        }
        System.out.print(" - ");
        System.out.print(alpha);
        System.out.print(" - ");
        System.out.print(redvalue);
        System.out.print(" - ");
        System.out.print(greenvalue);
        System.out.print(" - ");
        System.out.print(bluevalue);
        System.out.println();
    }
    
    @Override
    /**
     * Override clone method for polygon object to clone the vercices array
     */
    public Object clone() {
        try {
            Polygon temp = (Polygon) super.clone();
            temp.xvertices = xvertices.clone();
            temp.yvertices = yvertices.clone();
            return temp;
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(Polygon.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
